<?php
require_once 'phing/Task.php';
require_once 'phing/tasks/user/protobuf/parser/pb_parser.php';

/**
 * The protocol buffer phing task of protocol buffer of php,you can generate the stub classes for operating the protocol buffer *.pb files.
 *
 * @author ZhiQiang,He <developerworks@163.com>
 * @package phing.tasks.user
 * @since 2.4.1
 */
class ProtocolBufferTask extends Task
{
    /**
     * the source files
     *
     * @var  FileSet
     */
    protected $filesets    = array();
    /**
     * Whether the build should fail, if
     * errors occured
     *
     * @var boolean
     */
    protected $failonerror = false;
    /**
     * directory to put minified javascript files into
     *
     * @var  string
     */
    protected $targetDir;

    /**
     *  Nested creator, adds a set of files (nested fileset attribute).
     */
    public function createFileSet()
    {
        $num = array_push($this->filesets, new FileSet());
        return $this->filesets[$num - 1];
    }

    /**
     * Whether the build should fail, if an error occured.
     *
     * @param boolean $value
     */
    public function setFailonerror($value)
    {
        $this->failonerror = $value;
    }

    /**
     * sets the directory where minified javascript files should be put inot
     *
     * @param  string  $targetDir
     */
    public function setTargetDir($targetDir)
    {
        $this->targetDir = $targetDir;
    }

    /**
     * The init method: Do init steps.
     */
    public function init()
    {
        return true;
    }

    /**
     * The main entry point method.
     */
    public function main()
    {
        foreach ($this->filesets as $fs) {
            try {
                $files    = $fs->getDirectoryScanner($this->project)->getIncludedFiles();
                $fullPath = realpath($fs->getDir($this->project));
                foreach ($files as $file) {
                    $this->log('Generate php class from ' . $file);
                    try {

                        $target = $this->targetDir . '/' . str_replace($fullPath, '', str_replace('.proto', '.php', $file));
                        if (file_exists(dirname($target)) === false) {
                            mkdir(dirname($target), 0700, true);
                        }
                        $parser = new PBParser();
                        $parser->parse($fullPath . '/' . $file, $target);

                    } catch (ProtocolBufferException $pbe){
                        $this->log("Could not parse the file $file: " . $pbe->getMessage(), Project::MSG_ERR);
                    }
                }
            } catch (BuildException $be) {
                // directory doesn't exist or is not readable
                if ($this->failonerror) {
                    throw $be;
                } else {
                    $this->log($be->getMessage(), $this->quiet ? Project::MSG_VERBOSE : Project::MSG_WARN);
                }
            }
        }
    }
}
class ProtocolBufferException extends Exception {}
?>
